package com.hoo.util;

import java.io.*;
import com.hoo.common.constant.Constant;

public class ExtBatchRename {

	private static String pathString =Constant.WINDOSFILEPATH;
	/**
	 * 向指定的文件中添加指定的内容
	 * 
	 * @param filePath文件夹路径
	 * @param content
	 */
	public static void updateFile(String filePath, String fileName, String content) {
		File file = createFile(filePath, fileName);
		OutputStream os = null;
		try {
			os = new FileOutputStream(file);
			os.write(content.getBytes());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * 修改文件内容
	 * 
	 * @param filename
	 * @param oldcontent
	 * @param newContent
	 */
/*	public static void updateFileContent(String filename, String oldcontent,String newContent) {
		String fullPath = pathString+"/"+filename;
		String b = null;
		StringBuffer buffer = new StringBuffer();
		try {
			BufferedReader br= new BufferedReader( new FileReader(new File(fullPath)));
			int count=0;
			while ((b = br.readLine()) != null) {
				if (b.equals(oldcontent)) {
					buffer.append(b.replace(oldcontent, newContent)+"\r\n");
					count++;
				} else {
					buffer.append(b+"\r\n");
				}
			}
			if(count==0){buffer.append(newContent+"\r\n");}
			FileWriter fw = new FileWriter(fullPath);
			PrintWriter out = new PrintWriter(new BufferedWriter(fw));
            out.write(buffer.toString());
            out.flush();
            out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/
	public static void updateFileContent(String filename, String oldcontent,String newContent) {
		if(checkFile(filename)){
			String fullPath = pathString+"/"+filename;
			String b = null;
			StringBuffer buffer = new StringBuffer();
			try {
				BufferedReader br= new BufferedReader( new FileReader(new File(fullPath)));
				int count=0;
				while ((b = br.readLine()) != null) {
					if (b.equals(oldcontent)) {
						if(""!=newContent){
							buffer.append(b.replace(oldcontent, newContent)+"\r\n");
						}
						count++;
					} else {
						buffer.append(b+"\r\n");
					}
				}
				if(count==0){
					buffer.append(newContent+"\r\n");
				}
				FileWriter fw = new FileWriter(fullPath);
				PrintWriter out = new PrintWriter(new BufferedWriter(fw));
	            out.write(buffer.toString());
	            out.flush();
	            out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else {
			try {
				BufferedWriter bw = new BufferedWriter(new FileWriter(new File(pathString+"\\"+filename)));
				bw.write(newContent.toString());
				bw.flush();
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		
	}
	public static void main(String[] args) {
		//ExtBatchRename.updateFileContent("closeall.bat", "shutdown -m \\\\192.168.1.100-f -s -t 40", "shutdown -m \\\\192.168.1.100-f -s -t 50");
		//ExtBatchRename.reName(pathString, ".txt", ".bat");
		ExtBatchRename.updateFileName("close192.168.1.100.bat","close192.168.1.500.bat");
	}

	/**
	 * 修改文件名
	 * 
	 * @param filename
	 * @param toFilename
	 */
	public static void updateFileName(String filename, String toFilename) {
		File toBeRenamed = new File(pathString+"\\"+filename);
		if (!toBeRenamed.exists() || toBeRenamed.isDirectory()) {
			System.out.println("File does not exist: " + filename);
			return;
		}
		File newFile = new File(pathString+"\\"+toFilename);
		if (toBeRenamed.renameTo(newFile)) {
			System.out.println("File has been renamed.");
		} else {
			System.out.println("Error renmaing file");
		}
	}
	


	/**
	 * 创建文件
	 * 
	 * @param path
	 * @param fileName
	 */
	public static File createFile(String path, String fileName) {
		File f = new File(path);
		if (!f.exists()) {
			f.mkdirs();
		}
		File file = new File(f, fileName);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return file;
	}
	
	public static boolean checkFile(String fileName){
		File f = new File(pathString);
		if (!f.exists()) {
			return false;
		}
		File file = new File(f, fileName);
		if (!file.exists()) {
			return false;
		}
		return true;
	}

	
	/**
	 * 修改文件后缀名
	 * 
	 * @param path文件夹路径
	 * @param from
	 * @param to
	 */
	public static void reName(String path, String from, String to) {
		File f = new File(path);
		File[] fs = f.listFiles();
		for (int i = 0; i < fs.length; ++i) {
			File f2 = fs[i];
			if (f2.isDirectory()) {
				reName(f2.getPath(), from, to);
			} else {
		        try {
		        	String str="";
		        	StringBuffer buffer=new StringBuffer();
		        	BufferedReader br= new BufferedReader(new FileReader(f2));
		        	try {
						while ((str = br.readLine()) != null) {
							buffer.append(str+"\r\n");
						}
						BufferedWriter bw=new BufferedWriter(new FileWriter(new File(f2.getParent()+"\\"+f2.getName().substring(0,f2.getName().lastIndexOf("."))+".bat")));
						bw.write(buffer.toString());
						br.close();
						bw.flush();
						bw.close();
					} catch (IOException e1) {
						e1.printStackTrace();
					}

		        } catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}